<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页信息管理</title>
    <meta name="description" content="Free Bootstrap 4 Admin Theme | Pike Admin">

    <!-- Favicon -->
    <!--    <link rel="shortcut icon" href="background/images/favicon.ico">-->

    <link href="background/plugins/datetimepicker/css/daterangepicker.css" rel="stylesheet" />
    <!--引入adminkit模板-->
    <link href="adminkit/css/app.css" rel="stylesheet">

    <!-- END CSS for this page -->
</head>

<body >
<div class="wrapper" >
    <nav id="sidebar" class="sidebar">
        <div class="sidebar-content js-simplebar">
            <a class="sidebar-brand" href="#">
                <span class="align-middle">后台管理</span>
            </a>

            <ul class="sidebar-nav">

                <li class="sidebar-item ">
                    <a class="sidebar-link" th:href="@{/dashboard}">
                        <i class="align-middle" data-feather="sliders"></i> <span class="align-middle">首页</span>
                    </a>
                </li>


                <li class="sidebar-item ">
                    <a class="sidebar-link" href="/publishedArticleManage">
                        <i class="align-middle" data-feather="book"></i> <span class="align-middle">文章管理</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a class="sidebar-link" href="/kindManage">
                        <i class="align-middle" data-feather="credit-card"></i> <span class="align-middle">分类管理</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a class="sidebar-link" href="/tagManage">
                        <i class="align-middle" data-feather="tag"></i> <span class="align-middle">标签管理</span>
                    </a>
                </li>
                <li class="sidebar-item">
                    <a class="sidebar-link" th:href="@{/commentManage}">
                        <i class="align-middle" data-feather="message-square"></i> <span class="align-middle">评论管理</span>
                    </a>
                </li>
                <li class="sidebar-item active">
                    <a class="sidebar-link" href="/frontInfo">
                        <i class="align-middle" data-feather="airplay"></i> <span class="align-middle">外观管理</span>
                    </a>
                </li>
                <li class="sidebar-item">
                    <a class="sidebar-link" th:href="@{/statistics}">
                        <i class="align-middle" data-feather="box"></i> <span class="align-middle">统计</span>
                    </a>
                </li>
                <li class="sidebar-item ">
                    <a class="sidebar-link" href="/adminInfo">
                        <i class="align-middle" data-feather="user"></i> <span class="align-middle">个人信息管理</span>
                    </a>
                </li>
            </ul>


        </div>
    </nav>
    <div class="main">
        <!-- top bar navigation -->

        <nav class="navbar navbar-expand navbar-light navbar-bg">
            <a class="sidebar-toggle d-flex">
                <i class="hamburger align-self-center"></i>
            </a>
            <a href="/" class="btn btn-outline-info" disabled>返回前台首页</a>

            <div class="navbar-collapse collapse">
                <ul class="navbar-nav navbar-align">
                    <li class="nav-item dropdown">
                        <a class="nav-icon dropdown-toggle" href="#" id="messagesDropdown" data-toggle="dropdown">
                            <div class="position-relative">
                                <i class="align-middle" data-feather="message-square"></i>
                                <span class="indicator" >[[${session.unReadCount}]]</span>
                            </div>
                        </a>
                        <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right py-0" aria-labelledby="messagesDropdown">
                            <div class="dropdown-menu-header">
                                <div class="position-relative">
                                    [[${session.unReadCount}]]条新评论
                                </div>
                            </div>
                            <div class="list-group">
                                <a th:href="@{/commentManage}" class="list-group-item" th:each="comment:${session.unReadComment}">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col-2">
                                            <i class="text-danger" data-feather="alert-circle"></i>
                                        </div>
                                        <div class="col-10">
                                            <div class="text-dark">[[${comment.name}]]</div>
                                            <div class="text-muted small mt-1">[[${comment.content}]]</div>
                                            <div class="text-muted small mt-1">[[${ #dates.format(comment.time,'yyyy年MM月dd日 ')}]]</div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                            <div class="dropdown-menu-footer">
                                <a th:href="@{/commentManage}" class="text-muted">所有评论</a>
                            </div>
                        </div>
                    </li>

                    <li class="nav-item dropdown">
                        <a class="nav-icon dropdown-toggle d-inline-block d-sm-none" href="#" data-toggle="dropdown">
                            <i class="align-middle" data-feather="settings"></i>
                        </a>

                        <a class="nav-link dropdown-toggle d-none d-sm-inline-block" href="#" data-toggle="dropdown" >
                            <img th:src="${session.admin.img}" class="avatar img-fluid rounded mr-1"  th:alt="${session.admin.name}"/> <span class="text-dark">[[${session.admin.name}]]</span>                        </a>
                        <div class="dropdown-menu dropdown-menu-right">
                            <a class="dropdown-item" href="/logout">Log out</a>
                        </div>
                    </li>
                </ul>
            </div>
        </nav>

        <main class="content" >
            <div class="container-fluid p-0">

                <h1 class="h3 mb-3">首页信息管理</h1>

                <div class="row">
                    <div class="col-md-3 col-xl-2">

                        <div class="card">
                            <div class="list-group list-group-flush" role="tablist">

                                <a class="list-group-item list-group-item-action active" data-toggle="list"  href="#setFront" role="tab"  >
                                    主页信息管理
                                </a>
                                <a class="list-group-item list-group-item-action" data-toggle="list" href="#link" role="tab">
                                    友情链接管理
                                </a>
                                <a class="list-group-item list-group-item-action" data-toggle="list" href="#addLink" role="tab">
                                    新增友情链接
                                </a>

                            </div>
                        </div>
                    </div>

                    <div class="col-md-9 col-xl-10">
                        <div class="tab-content">
                            <div class="tab-pane fade show active" id="setFront" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">修改主页信息</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-4">
                                                <div class="form-group">
                                                    <label class="form-label"><span style="color: red">*</span> 背景图片</label>

                                                    <th:block th:if="${null == front}">
                                                        <img id="backImg" th:src="@{/background/images/img-upload.png}"
                                                             style="height: 100px;width: 500px;">
                                                    </th:block>
                                                    <th:block th:unless="${null == front}">
                                                        <img id="backImg" th:src="${front.getBackground()}"
                                                             style="width:500px ;height: 100px;display:block;">
                                                    </th:block>
                                                </div>
                                            </div>
                                            <div class="col-4"></div>
                                            <div class="col-4">
                                                <button class="btn btn-info" style="margin-bottom: 5px;"
                                                        id="uploadBackImg">
                                                    <i class="fa fa-picture-o"></i>&nbsp;上传
                                                </button>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="form-label"><span style="color: red">*</span>站点标题</label>
                                            <input type="text" id="title" class="form-control" th:value="${front.getTitle()}"/>
                                        </div>
                                        <div class="form-group">
                                            <label class="form-label">公告</label>
                                            <textarea id="notice" class="form-control"  >[[${front.notice}]]</textarea>
                                        </div>

                                        <div class="row">
                                            <div class="col-4">
                                                <div class="form-group">
                                                    <label class="form-label">公告图片</label>

                                                    <th:block th:if="${null == front}">
                                                        <img id="noticeImg" th:src="@{/background/images/img-upload.png}"
                                                             style="height: 64px;width: 64px;">
                                                    </th:block>
                                                    <th:block th:unless="${null == front}">
                                                        <img id="noticeImg" th:src="${front.getNoticeImg()}"
                                                             style="width:100px ;height: 100px;display:block;">
                                                    </th:block>
                                                </div>
                                            </div>
                                            <div class="col-4"></div>
                                            <div class="col-4">
                                                <button class="btn btn-info" style="margin-bottom: 5px;"
                                                        id="uploadNoticeImg">
                                                    <i class="fa fa-picture-o"></i>&nbsp;上传
                                                </button>
                                            </div>
                                        </div>



                                        <button type="button" class="btn btn-primary" onclick="setInfo()">提交</button>
                                        <a class="btn btn-primary" th:href="@{/frontInfo}">返回</a>
                                    </div>
                                </div>
                            </div>
                            <div class="tab-pane fade " id="link" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h6 class="card-title">友情链接</h6>
                                    </div>
                                    <table class="table" >
                                        <thead>
                                        <tr>
                                            <th style="width:25%;">图片</th>
                                            <th style="width:25%">名称</th>
                                            <th class="d-none d-md-table-cell" style="width:40%">链接地址</th>
                                            <th>Actions</th>
                                        </tr>
                                        </thead>
                                        <tbody th:each="link:${links}">
                                        <tr>
                                            <td>
                                                <img th:src="${link.getImg()}" width="48" height="48" class="rounded-circle mr-2" alt="Avatar">
                                            </td>
                                            <td th:text="${link.getName()}"></td>
                                            <td class="d-none d-md-table-cell" th:text="${link.getUrl()}"></td>
                                            <td class="table-action">
                                                <a th:href="@{/editLink(id=${link.getId()})}"><i class="align-middle" data-feather="edit-2"></i></a>
                                                <a th:onclick="deleteLink([[${link.id}]])"><i class="align-middle" data-feather="trash"></i></a>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="tab-pane fade" id="addLink" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">新增友情链接</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-4">
                                                <div class="form-group">
                                                    <label class="form-label">头像</label>

                                                    <th:block th:if="${null == link}">
                                                        <img id="linkImg" th:src="@{/background/images/img-upload.png}"
                                                             style="height: 64px;width: 64px;">
                                                    </th:block>
                                                    <th:block th:unless="${null == link}">
                                                        <img id="linkImg" th:src="${link.getImg()}"
                                                             style="width:100px ;height: 100px;display:block;">
                                                    </th:block>
                                                </div>
                                            </div>
                                            <div class="col-4"></div>
                                            <div class="col-4">
                                                <button class="btn btn-info" style="margin-bottom: 5px;"
                                                        id="uploadLinkImg">
                                                    <i class="fa fa-picture-o"></i>&nbsp;上传
                                                </button>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="form-label">名称</label>
                                            <input type="text" id="linkName" class="form-control"/>
                                        </div>

                                        <div class="form-group">
                                            <label class="form-label">URL</label>
                                            <input type="text" id="linkUrl" class="form-control" />
                                        </div>
                                        <button type="button" class="btn btn-primary" onclick="addLink()">提交</button>
                                        <a class="btn btn-primary" th:href="@{/frontInfo}">返回</a>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>

            </div>
        </main>

    </div>
</div>


<!-- END main -->

<!--引入adminkit模板-->
<script src="adminkit/js/app.js"></script>


<script src="common/js/sweetalert.min.js"></script>
<script src="common/js/common.js"></script>
<script src="common/js/public.js"></script>
<script src="background/plugins/datetimepicker/js/moment.min.js"></script>
<script src="background/plugins/datetimepicker/js/daterangepicker.js"></script>
<script src="background/ajaxupload/ajaxupload.js"></script>






<script type="text/javascript">
    //解决下拉菜单不显示
    $(function () {
        $('.dropdown-toggle').dropdown();
    })


    var backImgSrc=document.getElementById("backImg").getAttribute("src");
    $(function (){
        new AjaxUpload('uploadBackImg',{
            action:'/admin/image/uploads',
            name:'image',
            autoSubmit:true,
            responseType:'json',
            onSubmit: function (file, extension) {
                if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))) {
                    alert('只支持jpg、png、gif格式的文件！');
                    return false;
                }
            },
            onComplete: function (file, r) {
                if (r != null && r.code == 200) {
                    $("#backImg").attr("src", r.data);
                    // window.console.log(r.data);
                    // window.console.log($("#articleCoverImg").attr("src"));
                    backImgSrc=r.data;
                    //$("#Img").attr("style", "width: 128px;height: 128px;display:block;");
                    return false;
                } else {
                    alert("error");
                }
            }
        });
    });

    var linkImg;
    $(function (){
        new AjaxUpload('uploadLinkImg',{
            action:'/admin/image/uploads',
            name:'image',
            autoSubmit:true,
            responseType:'json',
            onSubmit: function (file, extension) {
                if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))) {
                    alert('只支持jpg、png、gif格式的文件！');
                    return false;
                }
            },
            onComplete: function (file, r) {
                if (r != null && r.code == 200) {
                    $("#linkImg").attr("src", r.data);
                    // window.console.log(r.data);
                    // window.console.log($("#articleCoverImg").attr("src"));
                    linkImg=r.data;
                    //$("#Img").attr("style", "width: 128px;height: 128px;display:block;");
                    return false;
                } else {
                    alert("error");
                }
            }
        });
    });


    var noticeImgSrc=document.getElementById("noticeImg").getAttribute("src");
    console.log(noticeImgSrc);
    $(function (){
        new AjaxUpload('uploadNoticeImg',{
            action:'/admin/image/uploads',
            name:'image',
            autoSubmit:true,
            responseType:'json',
            onSubmit: function (file, extension) {
                if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))) {
                    alert('只支持jpg、png、gif格式的文件！');
                    return false;
                }
            },
            onComplete: function (file, r) {
                if (r != null && r.code == 200) {
                    $("#noticeImg").attr("src", r.data);
                    // window.console.log(r.data);
                    // window.console.log($("#articleCoverImg").attr("src"));
                    noticeImgSrc=r.data;
                    $("#noticeImg").attr("style", "width: 128px;height: 128px;display:block;");
                    return false;
                } else {
                    alert("error");
                }
            }
        });
    });

    function setInfo(){
        var background=backImgSrc;
        var notice=$('#notice').val();
        var title=$('#title').val().trim();
        var noticeImg=noticeImgSrc;

        if(isNull(title)){
            swal("请输入标题",{
                icon:"error",
            });
            return ;
        }
        if(isNull(background)){
            swal("请选择背景图片",{
                icon:"error",
            });
            return ;
        }
        var front={
            "background":background,
            "notice":notice,
            "noticeImg":noticeImg,
            "title":title
        }

        $.ajax({
            url:'/setFrontInfo',
            dataType: 'json',
            type:'POST',
            contentType:'application/json',
            data:JSON.stringify(front),
            success: function (result) {
                console.log(result);
                //if (result.code == 200)
                if (result.code==200) {
                    console.log(result.code);
                    console.log(result.data);
                    swal({
                        title: "修改成功",
                        type: 'success',
                        showCancelButton: false,
                        confirmButtonColor: '#1baeae',
                        confirmButtonText: '返回',
                        confirmButtonClass: 'btn btn-success',
                        buttonsStyling: false
                    }).then(function () {
                        window.location.href = "/frontInfo";
                    })
                } else {
                    swal(result.message, {
                        icon: "error",
                    });
                }
                ;
            },
            error: function () {
                swal("操作失败", {
                    icon: "error",
                });
            }

        });
    }

    function addLink(){
        var img=linkImg;
        var name=$('#linkName').val().trim();
        var url=$('#linkUrl').val();

        var link={
            "img":img,
            "name":name,
            "url":url
        }

        $.ajax({
            url:'/addLink',
            dataType: 'json',
            type:'POST',
            contentType: 'application/json',
            data: JSON.stringify(link),
            success: function (result) {
                console.log(result);
                //if (result.code == 200)
                if (result.code==200) {
                    console.log(result.code);
                    console.log(result.data);
                    swal({
                        title: "添加成功",
                        type: 'success',
                        showCancelButton: false,
                        confirmButtonColor: '#1baeae',
                        confirmButtonText: '返回',
                        confirmButtonClass: 'btn btn-success',
                        buttonsStyling: false
                    }).then(function () {
                        window.location.href = "/frontInfo";
                    })
                } else {
                    swal(result.message, {
                        icon: "error",
                    });
                }
                ;
            },
            error: function () {
                swal("操作失败", {
                    icon: "error",
                });
            }

        });
    }

    function deleteLink(id){
        swal({
            title: "确定删除?",
            icon: 'warning',
            buttons:true,
            dangerMode:true,
        }).then((willDelete=>{
            if(willDelete){
                $.ajax({
                    type: 'POST',
                    url:'/deleteLink',
                    dataType:'json',
                    data:{id:id},
                    success:function (result) {
                        console.log(result);
                        if(result.code==200){
                            swal("删除成功", {
                                icon: "success",
                            }).then(function () {
                                window.location.href="/frontInfo";
                            })
                        }else {
                            swal(result.message, {
                                icon: "error",
                            });
                        }
                    }
                });
            }
        }));

    }




</script>
<!-- END Java Script for this page -->

</body>
</html>